package com.rc.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.rc.activity.R;
import com.rc.activity.dao.tbl_Report;
import com.rc.fragment.ReportDetailFragment;
import com.rc.util.Util;

public class ReportListAdapter extends BaseAdapter {

	private Context mContext;
	private ArrayList<tbl_Report> items;
	public boolean isShowCheck = false;
	private CheckBox cbMulti;
	private int color = 0;
	private String reportKey = "";
	private int start, end;

	public ReportListAdapter(Context c, ArrayList<tbl_Report> item,
			String reportKey, int start, int end) {
		this.start = start;
		this.end = end;
		mContext = c;
		this.items = item;
		this.reportKey = reportKey;
	}

	public int getCount() {
		return items.size();
	}

	public Object getItem(int position) {
		return items.get(position);
	}

	public long getItemId(int position) {
		return 0;
	}

	// create a new view for each item referenced by the Adapter
	@SuppressLint("ResourceAsColor")
	public View getView(final int position, View convertView, ViewGroup parent) {
		View v = convertView;
		TextView txtReportNameItem, txtReportTimeItem, txtReportMoneyApprovedItem, txtReportMoneyItem, txtReportType;
		LinearLayout llReportType;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			LayoutInflater vi = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.reportlist_item, null);

		}
		// Register
		llReportType = (LinearLayout) v.findViewById(R.id.llReportType);
		cbMulti = (CheckBox) v.findViewById(R.id.cbMulti);
		txtReportNameItem = (TextView) v.findViewById(R.id.txtReportNameItem);
		txtReportTimeItem = (TextView) v.findViewById(R.id.txtReportTimeItem);
		txtReportMoneyApprovedItem = (TextView) v
				.findViewById(R.id.txtReportMoneyApprovedItem);
		txtReportMoneyItem = (TextView) v.findViewById(R.id.txtReportMoneyItem);
		txtReportType = (TextView) v.findViewById(R.id.txtReportType);

		// Function
		txtReportNameItem.setText(items.get(position).getTitle());
		txtReportTimeItem.setText(items.get(position).getDate());
		txtReportMoneyApprovedItem.setText("$"
				+ Util.formatStringFloat(Util.convertFloat(items.get(position)
						.getApproved())));
		txtReportMoneyItem.setText("$"
				+ Util.formatStringFloat(Util.convertFloat((items.get(position)
						.getAmount()))));

		String mReportType = items.get(position).getReportType();

		if (mContext.getString(R.string.approved_reports).equals(mReportType)) {
			color = mContext.getResources().getColor(
					R.color.background_color_approved);
			llReportType.setBackgroundResource(R.drawable.item_approved_report);
		} else if (mContext.getString(R.string.draft_reports).equals(
				mReportType)) {
			color = mContext.getResources().getColor(
					R.color.background_color_draft);
			llReportType.setBackgroundResource(R.drawable.item_draft_report);
		} else if (mContext.getString(R.string.pending_reports).equals(
				mReportType)) {
			color = mContext.getResources().getColor(
					R.color.background_color_pendding);
			llReportType.setBackgroundResource(R.drawable.item_pending_report);
		} else if (mContext.getString(R.string.rejected_reports).equals(
				mReportType)) {
			color = mContext.getResources().getColor(
					R.color.background_color_reject);
			llReportType.setBackgroundResource(R.drawable.item_reject_report);
		} else if (mContext.getString(R.string.submitted_reports).equals(
				mReportType)) {
			color = mContext.getResources().getColor(
					R.color.background_color_submitted);
			llReportType
					.setBackgroundResource(R.drawable.item_submitted_report);
		}
		txtReportMoneyApprovedItem.setTextColor(color);
		String[] arrReportType = mReportType.split(mContext
				.getString(R.string.reports));
		txtReportType.setText(arrReportType[0]);
		if (!isShowCheck) {
			cbMulti.setVisibility(View.GONE);
		} else {
			cbMulti.setVisibility(View.VISIBLE);
		}
		if (items.get(position).isIsCheck()) {
			cbMulti.setChecked(true);
		} else {
			cbMulti.setChecked(false);
		}
		v.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (isShowCheck) {
					cbMulti = null;
					cbMulti = ((CheckBox) v.findViewById(R.id.cbMulti));
					if (cbMulti.isChecked()) {
						cbMulti.setChecked(false);
						items.get(position).setIsCheck(false);
					} else {
						items.get(position).setIsCheck(true);
						cbMulti.setChecked(true);
					}
				} else {
					Fragment fragment = new ReportDetailFragment(items.get(
							position).getReportID(), reportKey, start, end);
					Util.pushFragmentReceiptBox(fragment,
							(FragmentActivity) mContext, "2");
				}
			}
		});
		return v;
	}

	public void notiB(ArrayList<tbl_Report> arrReportObj, String reportKey,
			int start, int end) {
		this.items = arrReportObj;
		this.start = start;
		this.end = end;
		this.reportKey = reportKey;
		notifyDataSetChanged();
	}

}